package com.kura.uednews;

import com.kura.uedews.R;
import com.kura.uednews.fragment.fragment_news_list;
import com.kura.uednews.ultilities.Community;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.SimpleDrawerListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Community {
	// DAZIK
	private ActionBarDrawerToggle drawerListener;
	private DrawerLayout drawerLayout;
	private ListView listView;
	private MyAdapter myAdapter;
	private uednewsApplication application;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// get application
		application = (uednewsApplication) getApplication();
		//
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		// drawer layout
		drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
		drawerLayout.setDrawerListener(drawerListener);
		//
		drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {
			@Override
			public void onDrawerClosed(View drawerView) {
				// TODO Auto-generated method stub
				super.onDrawerClosed(drawerView);
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				// TODO Auto-generated method stub
				super.onDrawerOpened(drawerView);
			}
		};

		// listview
		listView = (ListView) findViewById(R.id.drawerList);
		myAdapter = new MyAdapter(this);
		listView.setAdapter(myAdapter);

		// listview handler

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int pos, long id) {
				application.setChoice(pos);
				// 1 là tất cả các chuyên mục của trường đã đăng kí
				// 7 dân trí
				// 8 các tin đã lưu
				if (pos == 1 || pos == 7) {
					FragmentTransaction tx = getFragmentManager()
							.beginTransaction();
					tx.replace(R.id.main, android.app.Fragment.instantiate(
							MainActivity.this,
							"com.kura.uednews.fragment.fragment_news_list"));
					tx.commit();
				} else if (pos == 2) {
					FragmentTransaction tx = getFragmentManager()
							.beginTransaction();
					tx.replace(R.id.main, android.app.Fragment.instantiate(
							MainActivity.this,
							"com.kura.uednews.fragment.fragment_cate_list"));
					tx.commit();
				} else if (pos == 7) { // tra cuu danh ba dien thoai
					FragmentTransaction tx = getFragmentManager()
							.beginTransaction();
					tx.replace(R.id.main, android.app.Fragment.instantiate(
							MainActivity.this,
							"com.kura.uednews.fragment.fragment_contact_list"));
					tx.commit();
				}else if (pos == 4){ // danh sach viec can lam
					FragmentTransaction tx = getFragmentManager()
							.beginTransaction();
					tx.replace(R.id.main, android.app.Fragment.instantiate(
							MainActivity.this,
							"com.kura.uednews.fragment.fragment_todo_list"));
					tx.commit();
				}
				if(pos == 1 || pos == 7 || pos == 3 || pos == 9 || pos == 5){
					drawerLayout.closeDrawer(listView);
				}
				
			}
		});

		getActionBar().setIcon(android.R.color.transparent);
		getActionBar().setTitle("");
		
		application.setChoice(7);
		FragmentTransaction tx = getFragmentManager()
				.beginTransaction();
		tx.replace(R.id.main, android.app.Fragment.instantiate(
				MainActivity.this,
				"com.kura.uednews.fragment.fragment_news_list"));
		tx.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		if (drawerListener.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		drawerListener.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		drawerListener.onConfigurationChanged(newConfig);
	}

	@Override
	public void sendData(String data) {
		// TODO Auto-generated method stub
		Toast.makeText(getApplicationContext(), data + " c", Toast.LENGTH_SHORT)
				.show();
	}

}

class MyAdapter extends BaseAdapter {

	private Context context;
	String[] menu;
	int[] images = { R.drawable.ic_news, 0, 0, R.drawable.ic_todo, 0,
			R.drawable.ic_bao, 0, R.drawable.ic_phone, 0 };

	public MyAdapter() {
		// TODO Auto-generated constructor stub
	}

	public MyAdapter(Context context) {
		// social = context.getResources().getStringArray(R.array.social);
		this.context = context;
		menu = context.getResources().getStringArray(R.array.menu);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return menu.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return menu[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.custom_news_row, parent, false);
		} else {
			row = convertView;
		}
		TextView cateId = (TextView) row.findViewById(R.id.cateId);
		TextView txt1 = (TextView) row.findViewById(R.id.titleNewsDetails);
		ImageView img1 = (ImageView) row.findViewById(R.id.imageView1);
		cateId.setText(position + "");
		txt1.setText(menu[position]);
		if (position == 0 || position == 3 || position == 5 || position == 7) {
			txt1.setBackground(context.getResources().getDrawable(
					R.drawable.black));
			img1.setImageResource(images[position]);
		} else {

		}

		return row;
	}
}
